package com.imooc.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * <h1></h1>
 *
 * @author : dzj
 * @create: 2022-02-15 21:10
 */
@Component
@Aspect
@Slf4j
public class ServiceLogAspect {

    @Pointcut("execution( * com.imooc.service.impl..*.*(..))")
    public void pointcut(){};

    @Around(value = "pointcut()")
    public Object recordTimeLock(ProceedingJoinPoint pjp) {
        Object proceed = null;
        Class<?> aClass = pjp.getTarget().getClass();
        MethodSignature method = (MethodSignature) pjp.getSignature();
        long start = System.currentTimeMillis();
        log.info("aspect began, {}.{}, time is : {}",aClass, method.getName(),start);
        try {
             proceed = pjp.proceed();

        } catch (Throwable e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        long gap = end - start;
        log.info("aspect began, {}.{}, time is : {}, time consume is : {}",aClass, method.getName(),end, gap);
        if(gap > 3000) {
            log.error("method end , time consuming : {}",gap);
        }else if(gap > 2000) {
            log.warn("method end , time consuming : {}",gap);
        }else {
            log.info("method end , time consuming : {}",gap);
        }
        return proceed;


    }
}
